package bal.android;

import bal.android.agente.ContratarAgenteActivity;
import bal.android.jogador.CadastrarJogadorActivity;
import bal.android.jogador.VisualizarJogadorActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class HomeActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		
		Button cadastrarJogadorButton = (Button) findViewById(R.id.buttonEntrarCadastroJogador);
		cadastrarJogadorButton.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View view) {
				iniciarCadastroJogador();
			}
		});

		Button visualizarJogadorButton = (Button) findViewById(R.id.buttonVisualizarJogador);
		visualizarJogadorButton.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View view) {
				iniciarVisualizacaoJogador();
			}
		});
		
		String response = ConexaoServidor.execute("VisualizarJogadorServlet", null, null);
		if (response.equals("erro")) {
			visualizarJogadorButton.setVisibility(View.INVISIBLE);
		} else {
			cadastrarJogadorButton.setVisibility(View.INVISIBLE);
		}

		Button logoutButton = (Button) findViewById(R.id.logout);
		logoutButton.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View view) {
				logout();
			}
		});
		
		Button listAgents = (Button) findViewById(R.id.buttonListarAgentes);
		
		listAgents.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				listarAgentes();
			}
			
		});	
	}

	private void listarAgentes() {
		Intent intent = new Intent(this, ContratarAgenteActivity.class);
		startActivity(intent);
		this.finish();
	}
	
	private void iniciarVisualizacaoJogador() {
		Intent intent = new Intent(this, VisualizarJogadorActivity.class);
		startActivity(intent);
		this.finish();
	}

	private void iniciarCadastroJogador() {
		Intent intent = new Intent(this, CadastrarJogadorActivity.class);
		startActivity(intent);
		this.finish();
	}

	private void logout() {
		ConexaoServidor.execute("LogoutServlet", null, null);
		Intent intent = new Intent(this, BalActivity.class);
		startActivity(intent);
		this.finish();
	}

}
